﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ManageBanking.Models;

namespace ManageBanking.Controllers
{
    public class T_AccountController : Controller
    {
        //
        // GET: /Card/

        T_AccountModels tam = new T_AccountModels();
        public ActionResult Cancel(string idtrans)
        {
            if (Session["Login"] != null && idtrans != null)
            {
                int id = Convert.ToInt32(idtrans);
                if (tam.Cancel(id) == true)
                    Session["ss"] = "true";
                else
                    Session["ss"] = "Delete Fail, Try Again";
                return RedirectToAction("CencelTransfer");
            
            }
            else
                return RedirectToAction("Home", "Login");

        }
        public ActionResult CencelTransfer()
        {

            if (Session["Login"] != null)
            {

                Username us = (Username)Session["Login"];
                if (us.IdTypeUser == 2)
                {
                    int total;

                    ViewData["menuleft_top"] = "CancelTransfer";
                    ViewData["info"] = tam.ListCanCel(us.IdUsername, out total);
                    ViewData["total"] = total == 0 ? 1 : total;
                    ViewData["ss"] = Session["ss"];
                    Session.Remove("ss");
                    return View();
                }
                else
                    return RedirectToAction("Home", "Login");

            }
            else
                return RedirectToAction("Home", "Login");
        }
        public ActionResult TypeAccount()
        {
            ViewData["menutopvalue"] = "Account";
            T_AccountModels cm = new T_AccountModels();
            ViewData["ltc"] = cm.ListAllAccount();
            return View();
        }

        public ActionResult Test(string idaccount, decimal money)
        {
            return View("Test");
        }
        public ActionResult ReceiveMoney()
        {
            if (Session["Login"] != null)
            {
                
                Username us = (Username)Session["Login"];
                if (us.IdTypeUser == 2)
                {
                    int total;

                    ViewData["menuleft_top"] = "ReceiveMoney";
                    ViewData["info"] = tam.ListReceive(us.IdUsername, out total);
                    ViewData["total"] = total == 0 ? 1 : total;
                    ViewData["ss"] = Session["ss"];
                    Session.Remove("ss");
                    return View();
                }
                else
                    return RedirectToAction("Home", "Login");
            
            }
            else
                return RedirectToAction("Home", "Login");
            
        }
        public ActionResult ResolveReceive(string pass,string idtrans)
        {
            if (Session["Login"] != null && pass != null && idtrans != null)
            {
                OnlineBankingDataContext odbc = new OnlineBankingDataContext();
                Username us = (Username)Session["Login"];
                if (us.IdTypeUser == 2)
                {
                    ReceiveMoney rs = odbc.ReceiveMoneys.Where(a => a.IdTransfer == Convert.ToInt32(idtrans)).First();

                    if (rs.PassTras == tam.getMD5(pass))
                    {
                        Session["ss"] = "true";
                        tam.UpdateReceive(Convert.ToInt32(idtrans));
                    }
                    else
                        Session["ss"] = "Wrong Password,retry";

                    return RedirectToAction("ReceiveMoney");
                }
                else
                    return RedirectToAction("Home", "Login");
       
            }
            else
                return RedirectToAction("Home", "Login");
        }
        
        public ActionResult Transfer(string idaccount,string money)
        {
            if (Session != null && idaccount != null && money != null)
            {
                Username us = (Username)Session["Login"];
                if (us.IdTypeUser == 2)
                {
                    ViewData["menuleft_top"] = "DetailAccount";
                    decimal money1 = Convert.ToDecimal(money);
                    ViewData["idaccount"] = idaccount;
                    ViewData["money"] = money1;
                    ViewData["ss"] = Session["ss"];
                    Session.Remove("ss");

                    return View("Transfer");
                }
                else
                    return RedirectToAction("Home", "Login");

            }
            else
                return RedirectToAction("Home", "Login");


        }

        public JsonResult CheckDate(FormCollection form)
        {
            System.Threading.Thread.Sleep(1000);
            string name = form["Date"];
            
            
            DateTime dateceive;
            try
            {
                dateceive = Convert.ToDateTime(name);
            }
            catch
            {
                return Json(false);
            }
            DateTime now = DateTime.Now;
            


            if (dateceive<now )

                return Json(false);
            else if (dateceive >now.AddDays(7))

                return Json(false);

            return Json(true);

        }
        public JsonResult CheckMoney(FormCollection form)
        {
            System.Threading.Thread.Sleep(1000);
            string name = form["money"];
            string ttmoney = form["ttmoney"];
            decimal ttmoney1 = Convert.ToDecimal(ttmoney);
            decimal money = 0;
            try
            {
                money = Convert.ToDecimal(name);
            }
            catch
            {
                return Json(false);
            }




            if (money < 100 || money > 20000 || money > ttmoney1)

                return Json(false);

            return Json(true);

        }
        public JsonResult CheckMoney1(FormCollection form)
        {
            System.Threading.Thread.Sleep(1000);
            string name = form["money"];
            decimal money = 0;
            try
            {
                money = Convert.ToDecimal(name);
            }
            catch
            {
                return Json(false);
            }




            if (money < 100 || money > 20000)

                return Json(false);

            return Json(true);

        }


        public JsonResult CheckIdAccount(FormCollection form)
        {
            System.Threading.Thread.Sleep(1000);

            string name = form["ToAccount"];
            string idacc = form["idacc"];
            OnlineBankingDataContext olbk = new OnlineBankingDataContext();
            int a = olbk.Account_Usernames.Where(b => b.IdAccount.Equals(name.Trim()) && b.IdAccount!=idacc).Count();
           

            if (a==0)

                return Json(false);

            return Json(true);

        }


        public ActionResult ResolveTransfer(string ToAccount, decimal money, decimal totalmoney, string idaccount,string pass)
        {
            if (Session["Login"] != null && idaccount != null && pass != null)
            {
                if (tam.CheckExistAcc(ToAccount) == false)
                {
                    Session["ss"] = "Account is not exist,try again";
                    return RedirectToAction("Transfer/" + idaccount + "/" + totalmoney, "T_Account");
                }
                else if (ToAccount == idaccount)
                {
                    Session["ss"] = "Destination Acc not the same with From Acc";
                    return RedirectToAction("Transfer/" + idaccount + "/" + totalmoney, "T_Account");
                }

                else if (totalmoney - money < 100)
                {
                    Session["ss"] = "Your Acc Must Have At Least 100 USD";
                    return RedirectToAction("Transfer/" + idaccount + "/" + totalmoney, "T_Account");

                }
                else
                {

                    string pass1 = tam.getMD5(pass);
                    tam.TransferMoney(idaccount, ToAccount, money, pass1);
                    decimal lessmoney = totalmoney - money;
                    Session["ss"] = "true";
                    return RedirectToAction("Transfer/" + idaccount + "/" + lessmoney, "T_Account");

                }
            }
            else
                return RedirectToAction("Home", "Login");

        }
     

        public ActionResult DetailAccount()
        {
            if (Session["Login"] != null)
            {
                ViewData["menuleft_top"] = "DetailAccount";
                Username us = (Username)Session["Login"];
                if (us.IdTypeUser == 2)
                {
                    ViewData["totaluser"] = tam.TotalUser(us.IdUsername) == 0 ? 1 : tam.TotalUser(us.IdUsername);
                    ViewData["info"] = tam.infoactive(us.IdUsername);
                    ViewData["money"] = tam.TotalMoney(us.IdUsername);
                    return View();
                }
                else
                    return RedirectToAction("Home", "Login");
        
            }
            else
                return RedirectToAction("Home", "Login");
        }

    }
}
